dojo.provide("msos.colortool_calc");
dojo.require("msos.common");
dojo.require("msos.tab");
dojo.require("msos.number_ctrl");
dojo.require("msos.validate");
dojo.require("dojo.cookie");
dojo.require("dojo.i18n");
msos.colortool_calc.generate_colortool=function(_1){
this.select_method="Rgb";
this.saved_color_elm=null;
this.web_safe_elm=null;
this.numeric_controls=[];
this.pointer_offset=3;
this.saved_rgb_colors=new Array();
this.current_rgb_color=new Array();
var _2=this;
dojo.requireLocalization("msos","colortool",djConfig.locale);
var _3=dojo.i18n.getLocalization("msos","colortool",djConfig.locale);
_1.ct_wheel_img.alt=_3.ct_wheel_img;
_1.rgb_r.title=_3.rgb_r+" (0 - 255)";
_1.hsb_h.title=_3.hsb_h+" (0 - 360)";
_1.rgb_g.title=_3.rgb_g+" (0 - 255)";
_1.hsb_s.title=_3.hsb_s+" (0 - 100)";
_1.rgb_b.title=_3.rgb_b+" (0 - 255)";
_1.hsb_v.title=_3.hsb_v+" (0 - 100)";
_1.ct_web_smart.title=_3.ct_web_smart;
_1.ct_update_main.value=_3.ct_update_main_value;
_1.ct_update_main.title=_3.ct_update_main_title;
_1.ct_hex_inp.title=_3.ct_hex_inp;
_1.ct_save_color.value=_3.ct_save_color_value;
_1.ct_save_color.title=_3.ct_save_color_title;
_1.ct_save_color.value=_3.ct_save_color_value;
_1.ct_save_color.title=_3.ct_save_color_title;
_1.ct_use_color.value=_3.ct_use_color_value;
_1.ct_use_color.title=_3.ct_use_color_title;
_1.ct_main_tab.style.overflow="hidden";
this.run_input_update=function(){
if(_2.select_method=="Rgb"){
_2.rgb_form_set_color();
}else{
if(_2.select_method=="Hsb"){
_2.hsb_form_set_color();
}else{
if(_2.select_method=="Hex"){
_2.hex_form_set_color();
}
}
}
console.debug("run_input_update -> method: "+_2.select_method);
};
this.set_hex_input=function(_4){
var _5;
_4=_4.toUpperCase();
if(toggle_dark_light(_4)){
_5="white";
}else{
_5="black";
}
_1.ct_hex_inp.style.backgroundColor="#"+_4;
_1.ct_hex_inp.style.color=_5;
_1.ct_hex_inp.value="#"+_4;
};
this.set_display=function(_6){
var _7;
_6=_6.toUpperCase();
if(toggle_dark_light(_6)){
_7="white";
}else{
_7="black";
}
_1.ct_display.style.backgroundColor="#"+_6;
_1.ct_display.style.color=_7;
_1.ct_display.innerHTML="#"+_6;
};
this.clear_saved_colors=function(){
_2.saved_rgb_colors=new Array();
dojo.cookie("ct_saved_color",null,{expires:-1});
_2.display_saved_colors();
};
this.clear_pointer=function(){
_1.black_circle.style.display="none";
_1.white_circle.style.display="none";
};
this.safe_tab_reset=function(){
if(_2.web_safe_elm){
_2.web_safe_elm.style.border="1px inset";
_2.web_safe_elm.style.cursor="crosshair";
}
};
this.set_pointer=function(){
var _8=rgb_to_hex(_2.current_rgb_color);
var _9=_2.get_pointer_coordinates();
if(toggle_dark_light(_8)){
_1.white_circle.style.left=_9.x+"px";
_1.white_circle.style.top=_9.y+"px";
_1.black_circle.style.display="none";
_1.white_circle.style.display="inline";
}else{
_1.black_circle.style.left=_9.x+"px";
_1.black_circle.style.top=_9.y+"px";
_1.black_circle.style.display="inline";
_1.white_circle.style.display="none";
}
console.debug("set_pointer -> left:"+_9.x+", top:"+_9.y);
};
this.update_input_form=function(_a,_b){
console.info("update_input_form -> Called!");
_2.clear_pointer();
_1.rgb_r.value=_a[0];
_1.rgb_g.value=_a[1];
_1.rgb_b.value=_a[2];
_1.hsb_h.value=_b[0];
_1.hsb_s.value=_b[1];
_1.hsb_v.value=_b[2];
var _c=rgb_to_hex(_a);
_2.set_hex_input(_c);
_2.set_display(_c);
_2.current_rgb_color=_a;
_2.set_pointer();
};
this.rgb_form_set_color=function(){
_2.rgb_set_color(_1.rgb_r.value,_1.rgb_g.value,_1.rgb_b.value);
};
this.hsb_form_set_color=function(){
_2.hsb_set_color(_1.hsb_h.value,_1.hsb_s.value,_1.hsb_v.value);
};
this.hex_form_set_color=function(){
var _d=msos.validate.check_hex_color(_1.ct_hex_inp.value);
if(!_d){
if(msos.common.var_is_null(_d)){
_2.set_hex_input(rgb_to_hex(_2.current_rgb_color));
}else{
alert(_d+" - ???");
}
_1.ct_hex_inp.focus();
return false;
}
_2.hex_set_color(_d);
return true;
};
this.hsb_set_color=function(h,s,v){
var rgb=hsv2rgb(h,s/100,v/100);
_2.update_input_form(rgb,new Array(h,s,v));
console.debug("hsb_set_color -> called update_input_form!");
};
this.rgb_set_color=function(r,g,b){
var hsb=rgb2hsv(r,g,b);
_2.update_input_form(new Array(r,g,b),new Array(parseInt(hsb.hue),parseInt(hsb.sat*100),parseInt(hsb.val*100)));
console.debug("rgb_set_color -> called update_input_form!");
};
this.hex_set_color=function(hex){
var r=msos.common.hex_to_dec(hex.substr(0,2));
var g=msos.common.hex_to_dec(hex.substr(2,2));
var b=msos.common.hex_to_dec(hex.substr(4,2));
_2.rgb_set_color(r,g,b);
console.debug("hex_set_color -> called rgb_set_color!");
};
this.update_safe_color=function(evt){
var tar=evt.target;
if(_2.web_safe_elm){
_2.web_safe_elm.style.border="1px inset";
_2.web_safe_elm.style.cursor="crosshair";
}
_2.web_safe_elm=tar;
tar.style.border="1px outset";
tar.style.cursor="default";
_2.hex_set_color(tar.update_color);
msos.common.event_debug(evt,"\nUpdate safe color:\nhex -> "+tar.style.backgroundColor);
};
this.onclick_saved_colors=function(evt){
var tar=evt.target;
if(_2.saved_color_elm){
_2.saved_color_elm.style.border="2px inset";
_2.saved_color_elm.style.cursor="crosshair";
}
_2.saved_color_elm=tar;
tar.style.border="2px outset";
tar.style.cursor="default";
_2.rgb_set_color(tar.r,tar.g,tar.b);
var _1e="\nSaved color click:\nR -> "+tar.r+"\nG -> "+tar.g+"\nB -> "+tar.b;
msos.common.event_debug(evt,_1e);
};
this.initiate_tabs=function(){
var _1f=new msos.tab.tool(_1.ct_tabs_div);
_1f.act_tab_style="tab_active";
_1f.pas_tab_style="tab_passive";
_1f.tab_cookie_name="colortool_tab";
_1f.tab_height_px=256;
var _20=function(){
_1f.go_to_tab();
};
_1f.get_tab_by_cookie();
var _21={caption:_3["ct_txt_6"],tab_title:_3["ct_txt_7"],container:_1.ct_main_tab};
var _22={caption:_3["ct_txt_8"],tab_title:_3["ct_txt_9"],container:_1.ct_websafe_tab};
var _23={caption:_3["ct_txt_10"],tab_title:_3["ct_txt_11"],container:_1.ct_saved_tab};
_1f.add_tab(_21);
_1f.add_tab(_22);
_1f.add_tab(_23);
_1f.generate_tabs();
_1f.tab_onclick=function(){
if(_1f.tab_set_active==1){
for(var num in _2.numeric_controls){
_2.numeric_controls[num].container_position();
}
}else{
if(_1f.tab_set_active==2){
_2.safe_tab_reset();
}
}
};
console.debug("initiate_tabs -> finished");
};
this.initiate_num_ctrls=function(){
var _25,_26,_27,_28,_29,_2a;
_25=new msos.number_ctrl.tool(_1.rgb_r);
_25.num_min_val=0;
_25.num_max_val=255;
_25.num_ctrl_left=-6;
_25.num_ctrl_top=-2;
_25.num_ctrl_id="rgb - red";
_25.after_change_function=_2.rgb_form_set_color;
_25.generate_num_ctrl();
_26=new msos.number_ctrl.tool(_1.rgb_g);
_26.num_min_val=0;
_26.num_max_val=255;
_26.num_ctrl_left=-6;
_26.num_ctrl_top=-2;
_26.num_ctrl_id="rgb - green";
_26.after_change_function=_2.rgb_form_set_color;
_26.generate_num_ctrl();
_27=new msos.number_ctrl.tool(_1.rgb_b);
_27.num_min_val=0;
_27.num_max_val=255;
_27.num_ctrl_left=-6;
_27.num_ctrl_top=-2;
_27.num_ctrl_id="rgb - blue";
_27.after_change_function=_2.rgb_form_set_color;
_27.generate_num_ctrl();
_28=new msos.number_ctrl.tool(_1.hsb_h);
_28.num_min_val=0;
_28.num_max_val=360;
_28.num_ctrl_left=-6;
_28.num_ctrl_top=-2;
_28.num_ctrl_id="hsb - hue";
_28.after_change_function=_2.hsb_form_set_color;
_28.generate_num_ctrl();
_29=new msos.number_ctrl.tool(_1.hsb_s);
_29.num_min_val=0;
_29.num_max_val=100;
_29.num_ctrl_left=-6;
_29.num_ctrl_top=-2;
_29.num_ctrl_id="hsb - sat";
_29.after_change_function=_2.hsb_form_set_color;
_29.generate_num_ctrl();
_2a=new msos.number_ctrl.tool(_1.hsb_v);
_2a.num_min_val=0;
_2a.num_max_val=100;
_2a.num_ctrl_left=-6;
_2a.num_ctrl_top=-2;
_2a.num_ctrl_id="hsb - bright";
_2a.after_change_function=_2.hsb_form_set_color;
_2a.generate_num_ctrl();
_2.numeric_controls=[_25.num_container,_26.num_container,_27.num_container,_28.num_container,_29.num_container,_2a.num_container];
};
this.generate_web_safe=function(){
var c=new Array("00","CC","33","66","99","FF");
var _2c=new Array();
for(var i=0;i<6;i++){
for(var j=0;j<6;j++){
for(var k=0;k<6;k++){
var l=c[i]+c[j]+c[k];
var _31=new Image();
_31.src=djConfig.msos_folder+"/images/shim.gif";
_31.className="ws_img";
_31.style.backgroundColor="#"+l;
_31.unselectable="on";
_31.alt="hex "+l;
_31.title="#"+l;
_31.update_color=l.toString();
dojo.connect(_31,"onclick",null,_2.update_safe_color);
_2c.push(_31);
}
}
}
for(var m=0;m<18;m++){
var _33=document.createElement("div");
_33.classname="ws_row";
var _34=_2c.splice(0,12);
for(var n=0;n<_34.length;n++){
_33.appendChild(_34[n]);
if(n==5){
_33.appendChild(document.createTextNode(" "));
}
}
_1.ct_websafe_tab.appendChild(_33);
}
console.debug("generate_web_safe -> finished");
};
this.display_saved_colors=function(){
if(_2.saved_rgb_colors.length==0){
_1.ct_saved_tab.innerHTML=_3["ct_txt_1"]+"<br />";
}else{
_1.ct_saved_tab.innerHTML=_3["ct_txt_2"]+"<br />";
var scd=[];
for(var i=0;i<_2.saved_rgb_colors.length;i++){
var clr=rgb_to_hex(_2.saved_rgb_colors[i]);
clr=clr.toUpperCase();
scd[i]=document.createElement("div");
scd[i].r=_2.saved_rgb_colors[i][0];
scd[i].g=_2.saved_rgb_colors[i][1];
scd[i].b=_2.saved_rgb_colors[i][2];
scd[i].className="ct_saved_colors";
scd[i].title="#"+clr;
scd[i].style.backgroundColor="#"+clr;
scd[i].unselectable="on";
if(toggle_dark_light(clr)){
scd[i].style.color="white";
}
dojo.connect(scd[i],"onclick",null,_2.onclick_saved_colors);
scd[i].innerHTML=_3["ct_txt_3"]+" "+scd[i].r+", "+scd[i].g+", "+scd[i].b;
_1.ct_saved_tab.appendChild(scd[i]);
}
var _39=document.createElement("input");
_39.type="button";
_39.value=_3["ct_txt_4"];
_39.title=_3["ct_txt_5"];
_39.unselectable="on";
dojo.connect(_39,"onclick",null,_2.clear_saved_colors);
_1.ct_saved_tab.appendChild(_39);
}
};
this.save_to_cookie=function(){
for(var i=0;i<_2.saved_rgb_colors.length;i++){
if((_2.saved_rgb_colors[i][0]==_2.current_rgb_color[0])&&(_2.saved_rgb_colors[i][1]==_2.current_rgb_color[1])&&(_2.saved_rgb_colors[i][2]==_2.current_rgb_color[2])){
return;
}
}
_2.saved_rgb_colors[_2.saved_rgb_colors.length]=_2.current_rgb_color;
var _3b=new Array;
for(var i=0;i<_2.saved_rgb_colors.length;i++){
_3b[_3b.length]=rgb_to_hex(_2.saved_rgb_colors[i]);
}
dojo.cookie("ct_saved_color",dojo.toJson(_3b),{expires:15});
_2.display_saved_colors();
};
this.start_colortool=function(){
var _3c="000000";
var _3d=new msos.common.init_popup(window);
if(!_3d.init_fields()){
alert(_3.ct_failed);
return false;
}
var _3e=dojo.fromJson(dojo.cookie("ct_saved_color"))||[];
for(var j=0;j<_3e.length;j++){
_2.saved_rgb_colors[_2.saved_rgb_colors.length]=hex_to_rgb_array(_3e[j]);
}
var _40=function(evt){
var pos=dojo.coords(evt.target);
var x=(evt.pageX-pos.l)-80;
var y=(evt.pageY-pos.t)-80;
var _45=calc_wheel_color(x,y);
if(_1.ct_web_smart.checked){
_3c=_45.charAt(0)+_45.charAt(0)+_45.charAt(2)+_45.charAt(2)+_45.charAt(4)+_45.charAt(4);
}else{
_3c=_45;
}
_2.set_hex_input(_3c);
return false;
};
var _46=function(evt){
_2.hex_set_color(_3c);
return false;
};
var _48=function(){
_2.set_hex_input(rgb_to_hex(_2.current_rgb_color));
return false;
};
dojo.connect(_1.ct_wheel_img,"onclick",null,_46);
dojo.connect(_1.ct_wheel_img,"onmouseout",null,_48);
dojo.connect(_1.ct_wheel_img,"onmousemove",null,_40);
var _49=function(){
_2.select_method="Hex";
};
var _4a=function(){
_2.select_method="Rgb";
};
var _4b=function(){
_2.select_method="Hsb";
};
dojo.connect(_1.ct_hex_inp,"onfocus",null,_49);
dojo.connect(_1.rgb_r,"onfocus",null,_4a);
dojo.connect(_1.rgb_g,"onfocus",null,_4a);
dojo.connect(_1.rgb_b,"onfocus",null,_4a);
dojo.connect(_1.hsb_h,"onfocus",null,_4b);
dojo.connect(_1.hsb_s,"onfocus",null,_4b);
dojo.connect(_1.hsb_v,"onfocus",null,_4b);
_1.ct_buttons.style.display="inline";
_1.ct_display.style.display="inline";
_1.ct_input_ref.style.display="inline";
var _4c=msos.validate.check_hex_color(_1.ct_hex_inp.value);
if(_4c){
_2.hex_set_color(_4c);
}else{
_2.rgb_set_color(210,26,210);
}
var _4d=rgb_to_hex(_2.current_rgb_color);
_1.ct_input_ref.style.backgroundColor="#"+_4d;
_1.ct_input_ref.title=_3["ct_txt_12"]+": #"+_4d;
dojo.connect(_1.ct_update_main,"onclick",null,_2.run_input_update);
dojo.connect(_1.ct_input_ref,"onclick",null,function(){
_2.hex_set_color(_4d);
});
dojo.connect(_1.ct_use_color,"onclick",null,_3d.popup_action);
dojo.connect(_1.ct_save_color,"onclick",null,_2.save_to_cookie);
_2.generate_web_safe();
_2.display_saved_colors();
_2.initiate_num_ctrls();
_2.initiate_tabs();
console.debug("start_colortool -> finished");
return true;
};
this.get_pointer_coordinates=function(){
var _4e=0;
var _4f=0;
var _50={x:0,y:0};
var pos=dojo.coords(_1.ct_wheel_img);
var _52=_2.current_rgb_color;
if((_52[0]==0)&&(_52[1]==0)&&(_52[2]==0)){
_50.x=0;
_50.y=0;
}else{
if((_52[0]==255)&&(_52[1]==255)&&(_52[2]==255)){
_50.x=-70;
_50.y=-70;
}else{
var hsv=rgb2hsv(_52[0],_52[1],_52[2]);
var _54=-1*((hsv.sat-2)/2);
var _55=hsv.val/2;
if(_55==0.5){
_4e=_54;
}else{
_4e=_55;
}
console.debug("get_pointer_coordinates -> hue:"+hsv.hue+", sat:"+hsv.sat+", value:"+hsv.val+", radius:"+_4e);
_50=calculateCartesian(_4e*80,hsv.hue);
}
}
console.debug("get_pointer_coordinates -> x:"+_50.x+", y:"+_50.y);
_50.x+=pos.l+80-_2.pointer_offset;
_50.y+=pos.t+80-_2.pointer_offset;
return _50;
};
};
function calculatePolar(x,y){
var qx=(x<0)?0:1;
var qy=(y<0)?0:1;
var _5a=2*qy+qx;
var _5b=new Array(180,360,180,0);
var _5c={r:0,theta:0};
_5c.r=Math.floor(Math.pow((Math.pow(x,2)+Math.pow(y,2)),0.5));
_5c.theta=_5b[_5a]+Math.floor(Math.atan(y/x)*360/2/Math.PI);
return _5c;
};
function calculateCartesian(r,_5e){
var _5f={x:0,y:0};
_5f.x=Math.floor(r*Math.cos(_5e*2*Math.PI/360));
_5f.y=Math.floor(r*Math.sin(_5e*2*Math.PI/360));
return _5f;
};
function calc_wheel_color(x,y){
var pol=calculatePolar(x,y);
var sat=0;
var val=0;
var rgb=new Array(0,0,0);
var _66="";
if(pol.r==0){
return rgb;
}else{
var _67=pol.r/80;
if(_67>1){
rgb=new Array(255,255,255);
sat=1;
val=1;
}else{
if(_67>=0.5){
sat=1-((_67-0.5)*2);
val=1;
rgb=hsv2rgb(pol.theta,sat,val);
}else{
sat=1;
val=_67*2;
rgb=hsv2rgb(pol.theta,sat,val);
}
}
}
_66=rgb_to_hex(rgb);
return _66;
};
function hsv2rgb(_68,S,V){
var R=255;
var G=255;
var B=255;
var H=_68/360;
if(S==0){
R*=V;
G*=V;
B*=V;
}else{
var _6f=H*6;
var _70=Math.floor(_6f);
var _71=V*(1-S);
var _72=V*(1-S*(_6f-_70));
var _73=V*(1-S*(1-(_6f-_70)));
if(_70==0){
var_r=V;
var_g=_73;
var_b=_71;
}else{
if(_70==1){
var_r=_72;
var_g=V;
var_b=_71;
}else{
if(_70==2){
var_r=_71;
var_g=V;
var_b=_73;
}else{
if(_70==3){
var_r=_71;
var_g=_72;
var_b=V;
}else{
if(_70==4){
var_r=_73;
var_g=_71;
var_b=V;
}else{
var_r=V;
var_g=_71;
var_b=_72;
}
}
}
}
}
R=var_r*255;
G=var_g*255;
B=var_b*255;
}
return new Array(Math.round(R),Math.round(G),Math.round(B));
};
function rgb2hsv(red,_75,_76){
red=red/255;
_75=_75/255;
_76=_76/255;
var _77=Math.max(red,Math.max(_75,_76));
var _78=Math.min(red,Math.min(_75,_76));
if(_77>0){
s=(_77-_78)/_77;
}else{
s=0;
}
if(s>0){
var _79=_77-_78;
var rc=(_77-red)/_79;
var gc=(_77-_75)/_79;
var bc=(_77-_76)/_79;
if(red==_77){
h=(bc-gc)/6;
}
if(_75==_77){
h=(2+rc-bc)/6;
}
if(_76==_77){
h=(4+gc-rc)/6;
}
}else{
h=0;
}
if(h<0){
h+=1;
}
return {"hue":Math.round(h*360),"sat":s,"val":_77};
};
function toggle_dark_light(_7d){
var _7e=0;
var _7f=_7d.substr(0,1);
var _80=_7d.substr(2,1);
var _81=_7d.substr(4,1);
_7f=msos.common.hex_eq_dec(_7f);
_80=msos.common.hex_eq_dec(_80);
_81=msos.common.hex_eq_dec(_81);
_7f=parseInt(_7f);
_80=parseInt(_80);
_81=parseInt(_81);
_7e=_7f+_80+_81;
if(_7e<13){
return true;
}
return false;
};
function hex_to_rgb_array(_82){
if(typeof (_82)=="undefined"){
return new Array(255,255,255);
}
var r=msos.common.hex_to_dec(_82.substr(0,2));
var g=msos.common.hex_to_dec(_82.substr(2,2));
var b=msos.common.hex_to_dec(_82.substr(4,2));
return new Array(r,g,b);
};
function rgb_to_hex(rgb){
if(typeof (rgb)=="undefined"){
return "FFFFFF";
}
return msos.common.dec_to_hex(rgb[0])+""+msos.common.dec_to_hex(rgb[1])+""+msos.common.dec_to_hex(rgb[2]);
};

